package com.huanghuai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.entity.SysMenu;
import com.huanghuai.entity.SysRoleMenu;
import com.huanghuai.entity.SysRoleUser;
import com.huanghuai.mapper.SysMenuMapper;
import com.huanghuai.service.SysMenuService;
import com.huanghuai.service.SysRoleMenuService;
import com.huanghuai.service.SysRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleUserService sysRoleUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<String> queryPermissionsByUserId(Integer userId) {
        LambdaQueryWrapper<SysRoleUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleUser::getUId,userId);
        List<SysRoleUser> sysRoleUsers = sysRoleUserService.list(queryWrapper);
        System.out.println("sysRoleUsers = " + sysRoleUsers);
        List<SysMenu> sysMenus = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (SysRoleUser sysRoleUser : sysRoleUsers) {
            LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRId,sysRoleUser.getRId());
            List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(sysRoleMenuLambdaQueryWrapper);
            for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
                LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                sysMenuLambdaQueryWrapper.eq(SysMenu::getId,sysRoleMenu.getMId());
                sysMenus= this.list(sysMenuLambdaQueryWrapper);
                for (SysMenu sysMenu : sysMenus) {
                    String sysMenuName = sysMenu.getCode();
                    name.add(sysMenuName);
                }
            }
        }
        return name;
    }
}
